package my.mybatis.common.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 
  * @ClassName: DateUtil
  * @Description: 日期操作
  * @author haodalong@xbniao.com
  * @date 2017-9-19 下午3:24:16
  *
 */
public class DateUtil {
  
	/**
	 *  日期形式字符串转date
	  * @param dateString 字符串日期
	  * @param dateType  日期格式 如: yyyy-MM-dd
	  * @return
	  * @throws Exception
	 */
    public static Date strToDate(String dateString,String dateType) throws Exception{
    	 SimpleDateFormat sdf = new SimpleDateFormat(dateType);  
		 Date date = sdf.parse(dateString);  
         return date;
    }
    /**
     * 
      * @param date 日期
      * @param dateType 日期格式 如: yyyy-MM-dd
      * @return
      * @throws Exception
     */
    public static String dateToStr(Date date,String dateType){
   	 SimpleDateFormat sdf = new SimpleDateFormat(dateType);  
       return sdf.format(date);
   }
    
	/**
	 *  日期月份计算
	  * @param date
	  * @return
	 */
    public static Date monthCount(Date date,int amount){
		Calendar calendar = Calendar.getInstance();/**日历对象**/
		calendar.setTime(date);/**设置当前日期**/
		calendar.add(Calendar.MONTH, amount);/**月份加减数**/
        return calendar.getTime();
    } 
    
	/**
	 *  日期天数计算
	  * @param date
	  * @return
	 */
    public static Date daysCount(Date date,int amount){
		Calendar calendar = Calendar.getInstance();/**日历对象**/
		calendar.setTime(date);/**设置当前日期**/
		calendar.add(Calendar.DAY_OF_MONTH, amount);/**天数加减数**/
        return calendar.getTime();
    }
	/**
	 *  判断是否区间：startDate <= date < endDate
	  * @param date
	  * @return
	 */
    public static boolean isIn(Date date, Date start,Date end){
    	if (date.getTime()>=start.getTime() && date.getTime() < end.getTime()) {
			return true;
		}
        return false;
    } 
    /**
      *  比较两个时间的大小
      * @param beginDate
      * @param endDate
      * @return 大于是1，等于的话是0，小于的话是-1
      * @throws ParseException
     */
    public static int compareDate(Date beginDate,Date endDate){
        /**compareTo的返回值:大于是1(beginDate大于endDate，后面类同)，等于的话是0，小于的话是-1**/
       return beginDate.compareTo(endDate);
    }
    /**
      * 获取年份
      * @param date
      * @return
     */
    public static int getYear(Date date){
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(date);/**设置当前日期**/
       return calendar.get(Calendar.YEAR);/**获取年份**/
    }
    /**
     * 获取月份
     * @param date
     * @return
    */
    public static int getMonth(Date date){
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(date);/**设置当前日期**/
    	 return calendar.get(Calendar.MONTH);/**获取月份**/
    }
    /**
     * 获取日期
     * @param date
     * @return
    */
    public static int getDay(Date date){
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(date);/**设置当前日期**/
    	return calendar.get(Calendar.DATE);/**获取年份**/
    }
	public static void main(String[] args) {
//		System.out.println(String.valueOf(DateUtil.getYear(new Date())).substring(2));
//		System.out.println(DateUtil.getMonth(new Date()));
//		System.out.println(new DecimalFormat("00").format(DateUtil.getMonth(new Date())));
//		System.out.println(DateUtil.getDay(new Date()));
		SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMdd");  
	    System.out.println(sdf.format(new Date()));
	       
	}
}
